package com.xxc.domain.strategy.service.rule.tree.factory.engine.impl;

import com.xxc.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import com.xxc.domain.strategy.model.valobj.RuleTreeNodeLineVO;
import com.xxc.domain.strategy.model.valobj.RuleTreeNodeVO;
import com.xxc.domain.strategy.model.valobj.RuleTreeVO;
import com.xxc.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.xxc.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.xxc.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @Author: xxc
 * @Description: 决策树引擎
 * @Date: 2025/1/29 19:46
 * @Version: 1.0
 */
@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {
    /**
     * 所有树节点的实现
     */
    private final Map<String, ILogicTreeNode> logicTreeNodeGroup;

    private final RuleTreeVO ruleTreeVO;

    public DecisionTreeEngine(Map<String, ILogicTreeNode> logicTreeNodeGroup, RuleTreeVO ruleTreeVO) {
        this.logicTreeNodeGroup = logicTreeNodeGroup;
        this.ruleTreeVO = ruleTreeVO;
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVO process(String userId, Long strategyId, Integer awardId) {
        DefaultTreeFactory.StrategyAwardVO strategyAwardData = null;

        // 获取基础信息
        String nextNode = ruleTreeVO.getTreeRootRuleNode();
        Map<String, RuleTreeNodeVO> treeNodeMap = ruleTreeVO.getTreeNodeMap();

        // 获取起始节点「根节点记录了第一个要执行的规则」
        RuleTreeNodeVO ruleTreeNode = treeNodeMap.get(nextNode);
        while (null != nextNode) {
            // 获取决策节点
            ILogicTreeNode logicTreeNode = logicTreeNodeGroup.get(ruleTreeNode.getRuleKey());
            String ruleValue = ruleTreeNode.getRuleValue();

            // 决策节点计算
            DefaultTreeFactory.TreeActionEntity logicEntity = logicTreeNode.logic(userId, strategyId, awardId, ruleValue);
            RuleLogicCheckTypeVO ruleLogicCheckTypeVO = logicEntity.getRuleLogicCheckType();    // 规则过滤状态
            strategyAwardData = logicEntity.getStrategyAwardVO();   // 接收结果数据
            log.info("决策树引擎【{}】treeId:{} node:{} code:{}", ruleTreeVO.getTreeName(), ruleTreeVO.getTreeId(), nextNode, ruleLogicCheckTypeVO.getCode());

            // 获取下个节点
            nextNode = nextNode(ruleLogicCheckTypeVO.getCode(), ruleTreeNode.getTreeNodeLineVOList());  // 节点中有子节点的信息
            ruleTreeNode = treeNodeMap.get(nextNode);
        }

        return strategyAwardData;
    }

    private String nextNode(String matterValue, List<RuleTreeNodeLineVO> treeNodeLineVOList) {
        // 1. 如果没有子节点, 返回null，也就是不继续了
        if (null == treeNodeLineVOList || treeNodeLineVOList.isEmpty()) return null;

        // 2. 对每一个决策分支线，根据分支线提供的校验方式，比对传过来的ruleLogicCheckTypeVO.getCode()的值
        for (RuleTreeNodeLineVO nodeLine : treeNodeLineVOList) {
            if (decisionLogic(matterValue, nodeLine)) {
                return nodeLine.getRuleNodeTo();
            }
        }
        // 3. 执行到这里说明当前连接线配置的节点都不符合条件，可以直接回去了
        // throw new RuntimeException("决策树引擎，nextNode 计算失败，未找到可执行节点！");
        return null;
    }

    public boolean decisionLogic(String matterValue, RuleTreeNodeLineVO nodeLine) {
        // 根据nodeLine配置的规则界限判断方式
        switch (nodeLine.getRuleLimitType()) {
            case EQUAL:
                return matterValue.equals(nodeLine.getRuleLimitValue().getCode());
            // 以下规则暂时不需要实现
            case GT:
            case LT:
            case GE:
            case LE:
            default:
                return false;
        }
    }

}
